package servlets.event;

import hibernate.method.EventMethods;
import hibernate.model.UserEvent;
import hibernate.util.HibernateUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.google.gson.Gson;

/**
 * Servlet implementation class addEventResult1
 */
@WebServlet("/addEventResult1")
public class addEventResult1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addEventResult1() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String local = (String) request.getParameter("local_id");
		int local_id = Integer.parseInt(local);
		response.setCharacterEncoding("UTF-8");
        List<UserEvent> list = new ArrayList<UserEvent>();
        List<String> list2 = new ArrayList<String>();
        SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		EventMethods em = new EventMethods();
		list = em.listUserEventByLocal(sessionFactory, session, local_id);
		//TableEntityMethods cm= new TableEntityMethods();
		//list = cm.listTablesByLocalId(sessionFactory, session, Integer.parseInt(local_id));

		for (UserEvent te1 :list){
			list2.add(String.valueOf(te1.getUserevent_id()));
		}
		
        String json = new Gson().toJson(list2);
        response.setContentType("application/json");
        response.getWriter().write(json);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		PrintWriter out = response.getWriter();
		
		
		String local = (String) request.getParameter("local_id");
		String defevent = (String) request.getParameter("defevent");
		
	}

}
